// 封装倒计时逻辑函数
import moment from 'moment'
import { ref, computed, onUnmounted } from 'vue'
export const useCountDown = () => {
  // 响应式数据
  const time = ref(0)
  const timer = null
  // 格式化分、秒
  const formatTime = computed(() => moment.unix(time.value).format('mm分ss秒'))
  // 开启倒计时的逻辑函数
  const start = (curretTime) => {
    time.value = curretTime
    timer = setInterval(() => {
      time.value--
    }, 1000)
  }
  // 组件销毁清除定时器
  onUnmounted(() => {
    timer && clearInterval(timer)
  })
  return {
    formatTime,
    start
  }
}